package com.hc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class BeanApplication {
    @Test
    public void f1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        //---------------------------->JDK动态代理,依赖于接口
        Teacher teacher = (Teacher) context.getBean("teacher");
        ExtraThing extraThing = (ExtraThing) context.getBean("extraThing");
        //动态代理回调类
        ProxyTeacher proxyTeacher = new ProxyTeacher(teacher,extraThing);
        //生成的动态代理类(实现了Teacher实现的接口)
        Teach proxyInstance = (Teach) Proxy.newProxyInstance(teacher.getClass().getClassLoader(), teacher.getClass().getInterfaces(), proxyTeacher);
        //动态代理类执行
        proxyInstance.teach();
        //-------------------------->CGLib动态代理,依赖于继承
        TransicationManager tx = (TransicationManager) context.getBean("transicationManager");
        CGLibProxy cgLibProxy = new CGLibProxy(teacher,tx);
        Teacher teacherProxy = (Teacher) cgLibProxy.getProxy();
        teacherProxy.teach();
        //--------------------------->AOP
        UserHelper userHelper = (UserHelper) context.getBean("userHelper");
        List<String> list = new ArrayList<>();
        list.add("insert");
        PermissionUtil.setPermissionList(list);
        userHelper.insertUser(); //有权限
        userHelper.insertUser2(); //无权限
    }
}
